home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / shadow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-05  |  5.8 KB  |  325 lines

  1. /*
  2.  * Copyright 1989, 1990, 1991, 1992, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include "shadow.h"
  13. #include "config.h"
  14. #include <stdio.h>
  15.  
  16. #ifdef    STDLIB_H
  17. #include <stdlib.h>
  18. #endif
  19.  
  20. #ifndef    BSD
  21. #include <string.h>
  22. #include <memory.h>
  23. #else
  24. #include <strings.h>
  25. #define    strchr    index
  26. #define    strrchr    rindex
  27. #endif
  28.  
  29. #ifdef    NDBM
  30. #include <ndbm.h>
  31. #include <fcntl.h>
  32. DBM    *sp_dbm;
  33. int    sp_dbm_mode = -1;
  34. static    int    dbmopened;
  35. static    int    dbmerror;
  36. #endif
  37.  
  38. #ifndef    lint
  39. static    char    sccsid[] = "@(#)shadow.c    3.10    20:38:10    3/7/92";
  40. #endif
  41.  
  42. static    FILE    *shadow;
  43. static    char    spwbuf[BUFSIZ];
  44. static    struct    spwd    spwd;
  45.  
  46. #define    FIELDS    9
  47. #define    OFIELDS    5
  48.  
  49. void
  50. setspent ()
  51. {
  52.     if (shadow)
  53.         rewind (shadow);
  54.     else
  55.         shadow = fopen (SHADOW, "r");
  56.  
  57.     /*
  58.      * Attempt to open the DBM files if they have never been opened
  59.      * and an error has never been returned.
  60.      */
  61.  
  62. #ifdef NDBM
  63.     if (! dbmerror && ! dbmopened) {
  64.         int    mode;
  65.         char    dbmfiles[BUFSIZ];
  66.  
  67.         strcpy (dbmfiles, SHADOW);
  68.         strcat (dbmfiles, ".pag");
  69.  
  70.         if (sp_dbm_mode == -1)
  71.             mode = O_RDWR;
  72.         else
  73.             mode = (sp_dbm_mode == O_RDWR) ? O_RDWR:O_RDONLY;
  74.  
  75.         if (! (sp_dbm = dbm_open (SHADOW, mode, 0)))
  76.             dbmerror = 1;
  77.         else
  78.             dbmopened = 1;
  79.     }
  80. #endif
  81. }
  82.  
  83. void
  84. endspent ()
  85. {
  86.     if (shadow)
  87.         (void) fclose (shadow);
  88.  
  89.     shadow = (FILE *) 0;
  90. #ifdef    NDBM
  91.     if (dbmopened && sp_dbm) {
  92.         dbm_close (sp_dbm);
  93.         sp_dbm = 0;
  94.     }
  95.     dbmopened = 0;
  96.     dbmerror = 0;
  97. #endif
  98. }
  99.  
  100. struct spwd *
  101. sgetspent (string)
  102. char    *string;
  103. {
  104.     char    *fields[FIELDS];
  105.     char    *cp;
  106.     char    *cpp;
  107.     int    atoi ();
  108.     long    atol ();
  109.     int    i;
  110.  
  111.     strncpy (spwbuf, string, BUFSIZ-1);
  112.     spwbuf[BUFSIZ-1] = '\0';
  113.  
  114.     if (cp = strrchr (spwbuf, '\n'))
  115.         *cp = '\0';
  116.  
  117.     for (cp = spwbuf, i = 0;*cp && i < FIELDS;i++) {
  118.         fields[i] = cp;
  119.         while (*cp && *cp != ':')
  120.             cp++;
  121.  
  122.         if (*cp)
  123.             *cp++ = '\0';
  124.     }
  125.     if (i == (FIELDS-1))
  126.         fields[i++] = cp;
  127.  
  128.     if (*cp || (i != FIELDS && i != OFIELDS))
  129.         return 0;
  130.  
  131.     spwd.sp_namp = fields[0];
  132.     spwd.sp_pwdp = fields[1];
  133.  
  134.     if ((spwd.sp_lstchg = strtol (fields[2], &cpp, 10)) == 0 && *cpp)
  135.         return 0;
  136.     else if (fields[2][0] == '\0')
  137.         spwd.sp_lstchg = -1;
  138.  
  139.     if ((spwd.sp_min = strtol (fields[3], &cpp, 10)) == 0 && *cpp)
  140.         return 0;
  141.     else if (fields[3][0] == '\0')
  142.         spwd.sp_min = -1;
  143.  
  144.     if ((spwd.sp_max = strtol (fields[4], &cpp, 10)) == 0 && *cpp)
  145.         return 0;
  146.     else if (fields[4][0] == '\0')
  147.         spwd.sp_max = -1;
  148.  
  149.     if (i == OFIELDS) {
  150.         spwd.sp_warn = spwd.sp_inact = spwd.sp_expire =
  151.             spwd.sp_flag = -1;
  152.  
  153.         return &spwd;
  154.     }
  155.     if ((spwd.sp_warn = strtol (fields[5], &cpp, 10)) == 0 && *cpp)
  156.         return 0;
  157.     else if (fields[5][0] == '\0')
  158.         spwd.sp_warn = -1;
  159.  
  160.     if ((spwd.sp_inact = strtol (fields[6], &cpp, 10)) == 0 && *cpp)
  161.         return 0;
  162.     else if (fields[6][0] == '\0')
  163.         spwd.sp_inact = -1;
  164.  
  165.     if ((spwd.sp_expire = strtol (fields[7], &cpp, 10)) == 0 && *cpp)
  166.         return 0;
  167.     else if (fields[7][0] == '\0')
  168.         spwd.sp_expire = -1;
  169.  
  170.     if ((spwd.sp_flag = strtol (fields[8], &cpp, 10)) == 0 && *cpp)
  171.         return 0;
  172.     else if (fields[8][0] == '\0')
  173.         spwd.sp_flag = -1;
  174.  
  175.     return (&spwd);
  176. }
  177.  
  178. struct spwd
  179. *fgetspent (fp)
  180. FILE    *fp;
  181. {
  182.     char    buf[BUFSIZ];
  183.  
  184.     if (! fp)
  185.         return (0);
  186.  
  187.     if (fgets (buf, BUFSIZ, fp) == (char *) 0)
  188.         return (0);
  189.  
  190.     return sgetspent (buf);
  191. }
  192.  
  193. struct spwd
  194. *getspent ()
  195. {
  196.     if (! shadow)
  197.         setspent ();
  198.  
  199.     return (fgetspent (shadow));
  200. }
  201.  
  202. struct spwd
  203. *getspnam (name)
  204. #if    __STDC__
  205. const
  206. #endif
  207. char    *name;
  208. {
  209.     struct    spwd    *sp;
  210. #ifdef NDBM
  211.     datum    key;
  212.     datum    content;
  213. #endif
  214.  
  215.     setspent ();
  216.  
  217. #ifdef NDBM
  218.  
  219.     /*
  220.      * If the DBM file are now open, create a key for this UID and
  221.      * try to fetch the entry from the database.  A matching record
  222.      * will be unpacked into a static structure and returned to
  223.      * the user.
  224.      */
  225.  
  226.     if (dbmopened) {
  227.         key.dsize = strlen (name);
  228.         key.dptr = name;
  229.  
  230.         content = dbm_fetch (sp_dbm, key);
  231.         if (content.dptr != 0) {
  232.             memcpy (spwbuf, content.dptr, content.dsize);
  233.             spw_unpack (spwbuf, content.dsize, &spwd);
  234.             return &spwd;
  235.         }
  236.     }
  237. #endif
  238.     while ((sp = getspent ()) != (struct spwd *) 0) {
  239.         if (strcmp (name, sp->sp_namp) == 0)
  240.             return (sp);
  241.     }
  242.     return (0);
  243. }
  244.  
  245. int
  246. putspent (sp, fp)
  247. #if    __STDC__
  248. const
  249. #endif
  250. struct    spwd    *sp;
  251. FILE    *fp;
  252. {
  253.     int    errors = 0;
  254.  
  255.     if (! fp || ! sp)
  256.         return -1;
  257.  
  258.     if (fprintf (fp, "%s:%s:", sp->sp_namp, sp->sp_pwdp) < 0)
  259.         errors++;
  260.  
  261.     if (sp->sp_lstchg != -1) {
  262.         if (fprintf (fp, "%ld:", sp->sp_lstchg) < 0)
  263.             errors++;
  264.     } else if (putc (':', fp) == EOF)
  265.         errors++;
  266.  
  267.     if (sp->sp_min != -1) {
  268.         if (fprintf (fp, "%ld:", sp->sp_min) < 0)
  269.             errors++;
  270.     } else if (putc (':', fp) == EOF)
  271.         errors++;
  272.  
  273.     if (sp->sp_max != -1) {
  274.         if (fprintf (fp, "%ld", sp->sp_max) < 0)
  275.             errors++;
  276.     }
  277.  
  278.     /*
  279.      * See if the structure has any of the SVR4 fields in
  280.      * it.  If none of those fields have any data there is
  281.      * no reason to write them out since they will be filled
  282.      * in the same way when they are read back in.  Otherwise
  283.      * there is at least one SVR4 field that must be output.
  284.      */
  285.  
  286.     if (sp->sp_warn == -1 && sp->sp_inact == -1 &&
  287.             sp->sp_expire == -1 && sp->sp_flag == -1) {
  288.         if (putc ('\n', fp) == EOF || errors)
  289.             return -1;
  290.         else
  291.             return 0;
  292.     } else if (putc (':', fp) == EOF)
  293.         errors++;
  294.  
  295.     if (sp->sp_warn != -1) {
  296.         if (fprintf (fp, "%ld:", sp->sp_warn) < 0)
  297.             errors++;
  298.     } else if (putc (':', fp) == EOF)
  299.         errors++;
  300.  
  301.     if (sp->sp_inact != -1) {
  302.         if (fprintf (fp, "%ld:", sp->sp_inact) < 0)
  303.             errors++;
  304.     } else if (putc (':', fp) == EOF)
  305.         errors++;
  306.  
  307.     if (sp->sp_expire != -1) {
  308.         if (fprintf (fp, "%ld:", sp->sp_expire) < 0)
  309.             errors++;
  310.     } else if (putc (':', fp) == EOF)
  311.         errors++;
  312.  
  313.     if (sp->sp_flag != -1) {
  314.         if (fprintf (fp, "%ld", sp->sp_flag) < 0)
  315.             errors++;
  316.     }
  317.     if (putc ('\n', fp) == EOF)
  318.         errors++;
  319.  
  320.     if (errors)
  321.         return -1;
  322.     else
  323.         return 0;
  324. }
  325.